<?php
namespace app\controller;

use app\BaseController;
use app\model\ActivityGoods;
use app\traits\CommonTrait;
use think\facade\Request;

class AdminGoods extends BaseController
{
    use CommonTrait;
    public function goodsList(){
        $currentPage = Request::get('current_page') ? Request::get('current_page') : 1;
        $perPage = $this->getPerPage();

        $goodsType = Request::get('goods_type',0);
        //商品名称或商品id
        $goodsInfo = Request::get('goods_info', '');
        //商家名称或商家id
        $shopInfo = Request::get('shop_info', '');
        $where = [];
        $where[] = ['audit_status', '=', 1];

        if ($goodsInfo) {
            $where[] = ['product_name|product_id', 'like', "%$goodsInfo%"];
        }
        if ($shopInfo) {
            $where[] = ['shop_name|shop_id', 'like', "%$shopInfo%"];
        }
        if($goodsType)
        {
            $where[] = ['goods_type','=',$goodsType];
        }
        $goodsList = ActivityGoods::where($where)
            ->field('id,apply_id,product_img,product_name,product_price,product_id,shop_name,shop_id,shop_phone,is_new,dsr,product_stock,
            common_commission_rate,commission_rate,service_rate,promotion_price,promotion_stock,
            commission_effect_start_time,commission_effect_end_time,promotion_url,mini_show,goods_type')
            ->order('apply_id', 'desc')
            ->paginate(['page' => $currentPage, 'list_rows' => $perPage]);
        return $this->apiJson(1,'success',$goodsList);
    }

    //设置商品显示不显示在小程序
    public function setGoodsShowHide()
    {
        $goodsId = Request::post('goods_id');
        if(!$goodsId){
            return $this->apiJson(-1,'缺少商品id参数');
        }
        $goods = ActivityGoods::where(['status'=>1,'id'=>$goodsId])->field('id,mini_show')->findOrEmpty();
        if($goods->isEmpty()){
            return $this->apiJson(-1,'商品不存在或已下架');
        }
        if($goods['mini_show'] == 0){
            $show = 1;
        }else{
            $show = 0;
        }
        ActivityGoods::where(['status'=>1,'id'=>$goodsId])->update(['mini_show'=>$show]);
        return $this->apiJson(1,'success');
    }

    public function setMultiGoodsShowHide()
    {
        $goodsId = Request::post('goods_id');
        $show = Request::post('show',1);
        if(!$goodsId){
            return $this->apiJson(-1,'缺少商品id参数');
        }
        if($show!=1&&$show!=0){
            return $this->apiJson(-1,'参数错误');
        }
        ActivityGoods::where(['status'=>1])->whereIn('id',$goodsId)->update(['mini_show'=>$show]);
        return $this->apiJson(1,'success');
    }
}